package cn.com.cfae.iras.doc.analyze.parse.model;

import cn.com.cfae.iras.doc.analyze.parse.kv.TextLayout;
import cn.com.cfae.iras.doc.analyze.parse.model.table.TableItem;
import org.apache.commons.collections.CollectionUtils;

import java.util.*;

public class ChapterModel extends BizBaseModel {

    public ChapterModel createChildChapterModel(String tableId) {
        ChapterModel chapterModel = getChildChapterModel(tableId);
        if (chapterModel == null) {
            chapterModel = new ChapterModel();
            chapterModel.setChapterId(tableId);
            addChild(chapterModel);
            setParent(this);
        }
        return chapterModel;
    }

    public List<SectionModel> getSectionModelList() {
        return sectionModelList;
    }

    public void setSectionModelList(List<SectionModel> sectionModelList) {
        this.sectionModelList = sectionModelList;
    }

    private List<SectionModel> sectionModelList;

    public CatalogModel getLastCatalogModel() {
        return getCatalogModels().get(getCatalogModels().size() - 1);
    }

    public List<CatalogModel> getCatalogModels() {
        return catalogModels;
    }


    public void setCatalogModels(List<CatalogModel> catalogModels) {
        this.catalogModels = catalogModels;
    }

    /**
     * M0、BM0、DM0、BDM0会存在目录结构对象
     */
    private List<CatalogModel> catalogModels;


    public ChapterModel() {
        setPageModelList(new ArrayList<>());
        setCatalogModels(new ArrayList<>());
        setSectionModelList(new ArrayList<>());
    }

    /**
     * 实际序号
     */
    private int chapterSeqNo;


    public TableItem getTableItem() {
        return tableItem;
    }

    public void setTableItem(TableItem tableItem) {
        this.tableItem = tableItem;
        if (tableItem != null) {
            this.setChapterId(tableItem.getItemNo());
        }
    }


    private TableItem tableItem;


    public String getChapterTitle() {
        return chapterTitle;
    }

    public void setChapterTitle(String chapterTitle) {
        this.chapterTitle = chapterTitle;
    }

    /**
     * 章节标题，实际标题
     */
    String chapterTitle;

    public TableType getTableType() {
        return tableType;
    }

    public void setTableType(TableType tableType) {
        this.tableType = tableType;
    }


    /**
     * @return
     */
    public String getChapterId() {
        return chapterId;
    }

    public void setChapterId(String chapterId) {
        this.chapterId = chapterId;
    }

    public int getStartPage() {
        return startPage;
    }

    public void setStartPage(int startPage) {
        this.startPage = startPage;
    }

    public int getEndPage() {
        return endPage;
    }

    public void setEndPage(int endPage) {
        this.endPage = endPage;
    }

    public int getDepth() {
        return depth;
    }

    public void setDepth(int depth) {
        this.depth = depth;
    }

    public ChapterModel getParent() {
        return parent;
    }

    public void setParent(ChapterModel parent) {
        this.parent = parent;
    }

    public List<ChapterModel> getChildrens() {
        return childrens;
    }

    public void setChildrens(List<ChapterModel> childrens) {
        this.childrens = childrens;
    }

    /**
     * M、BM、DM、BDM
     */
    TableType tableType;


    /**
     * 章节ID
     */
    String chapterId;
    /**
     * 起始页码
     */
    int startPage;
    /**
     * 截止页码
     */
    int endPage;

    /**
     * 目录层级
     * 目前检查2级
     */
    int depth;


    /**
     * 上级目录
     */
    private ChapterModel parent;
    /**
     * 子目录
     */
    private List<ChapterModel> childrens;


    public void addChild(ChapterModel child) {
        if (childrens == null) {
            childrens = new ArrayList<>();
        }
        childrens.add(child);
    }

    private TextLayout textLayout;

    public TextLayout getTextLayout() {
        return textLayout;
    }

    public void setTextLayout(TextLayout textLayout) {
        this.textLayout = textLayout;
    }

    public int getChapterSeqNo() {
        return chapterSeqNo;
    }

    public void setChapterSeqNo(int chapterSeqNo) {
        this.chapterSeqNo = chapterSeqNo;
    }

    public List<PageModel> getPageModelList() {
        return pageModelList;
    }

    public void setPageModelList(List<PageModel> pageModelList) {
        this.pageModelList = pageModelList;
    }

    private List<PageModel> pageModelList;


    public void addPageModel(PageModel pageModel) {
        pageModelList.add(pageModel);
    }


    public Collection<ExtractBizModel> getExtractBizModelList() {
        return extractBizModelMap.values();
    }


    public Map<String, ExtractBizModel> getExtractBizModelMap() {
        return extractBizModelMap;
    }

    public void setExtractBizModelMap(Map<String, ExtractBizModel> extractBizModelMap) {
        this.extractBizModelMap = extractBizModelMap;
    }

    private Map<String, ExtractBizModel> extractBizModelMap = new HashMap<>();


    public ExtractBizModel getModel(String modelName) {
        return extractBizModelMap.get(modelName);
    }

    /**
     * 后续需要调整，先复用接口
     *
     * @param modelName
     * @return
     */
    public ExtractBizModel getTableModel(String modelName) {
        return getModel(modelName);
    }

    public ChapterModel getChildChapterModel(String childChapterId) {
        ChapterModel targetChapterModel = null;
        List<ChapterModel> chapterModelList = getChildrens();
        if (CollectionUtils.isEmpty(chapterModelList)) return null;
        for (ChapterModel chapterModel : chapterModelList) {
            if (childChapterId.equalsIgnoreCase(chapterModel.getChapterId())) {
                targetChapterModel = chapterModel;
                break;
            }
        }
        return targetChapterModel;
    }

    private int bottomPageNum;

    public int getBottomPageNum() {
        return bottomPageNum;
    }

    public void setBottomPageNum(int bottomPageNum) {
        this.bottomPageNum = bottomPageNum;
    }

    public void addSectionModel(SectionModel sectionModel) {
        sectionModelList.add(sectionModel);
    }

    public SectionModel getLastSectionModel() {
        if(getSectionModelList().isEmpty()){
            return null;
        }
        int size = getSectionModelList().size();
        return getSectionModelList().get(size - 1);
    }

}
